import platform

import distro

from dataclasses import dataclass, asdict
import json


@dataclass
class SystemInfo(object):
    def __init__(self):
        self.os = ''
        self.system = ''
        self.version = ''
        self.codename = ''
        self.cpu = ''


def get_system_info() -> SystemInfo:
    system_info = SystemInfo()
    system_info.os = platform.system().lower()
    # 获取机器的硬件架构
    machine = platform.machine().lower()

    # 根据常见的架构名称进行映射
    if machine in ['x86_64', 'amd64']:
        system_info.cpu = 'amd64'
    elif machine in ['arm64', 'aarch64']:
        system_info.cpu = 'arm64'
    else:
        system_info.cpu = machine

    # 获取具体的系统名称和版本
    if system_info.os == 'linux':
        # 对于 Linux 系统，尝试获取更详细的发行版信息
        try:
            # 使用 platform.linux_distribution() 已被废弃，推荐使用 distro 模块
            # 如果没有安装 distro 模块，可以使用 pip install distro 来安装
            system_info.system = distro.id()
            system_info.version = distro.version()
            system_info.codename = distro.codename()
        except ImportError:
            print("distro 模块未安装，无法获取详细 Linux 发行版信息")
    elif system_info.os == 'windows':
        system_info.system = platform.win32_ver()[0]
        system_info.version = platform.win32_ver()[1]
    elif system_info.os == 'darwin':
        system_info.system = 'macos'
        system_info.version = platform.mac_ver()[0]
    else:
        system_info.system = platform.system()
        system_info.version = platform.release()

    return system_info


if __name__ == '__main__':
    print(json.dumps(asdict(get_system_info()), indent=2))
